package com.cloudsigns.factorymanager2.engineering.customview;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.PhotoBean;
import com.cloudsigns.factorymanager2.engineering.utils.PhotoUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/4/24.
 */

public class PhotoView extends LinearLayout {
    public static final int MAX_NUMBER = 3;
    private Context context;
    private LayoutInflater layoutInflater;
    private View parentView;
    private ImageView[] imageViewAry = new ImageView[3];
    private ImageView[] addViewAry = new ImageView[3];
    private View[] bgArr = new View[3];
    private List<PhotoBean> photoList;
    private OnPhotoViewClickListener photoViewClickListener;

    public PhotoView(Context context) {
        super(context);
        this.context = context;
    }

    public PhotoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        parentView = layoutInflater.inflate(R.layout.view_photo, null);

        imageViewAry[0] = parentView.findViewById(R.id.img1);
        imageViewAry[1] = parentView.findViewById(R.id.img2);
        imageViewAry[2] = parentView.findViewById(R.id.img3);

        addViewAry[0] = parentView.findViewById(R.id.img_add_1);
        addViewAry[1] = parentView.findViewById(R.id.img_add_1);
        addViewAry[2] = parentView.findViewById(R.id.img_add_1);

        bgArr[0] = parentView.findViewById(R.id.rl_photographs_bg_1);
        bgArr[1] = parentView.findViewById(R.id.rl_photographs_bg_2);
        bgArr[2] = parentView.findViewById(R.id.rl_photographs_bg_3);

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        addView(parentView, params);

    }


    public void setPhotoViewClickListener(OnPhotoViewClickListener clickListener) {
        photoViewClickListener = clickListener;
        addListener();
    }


    public void setData(Activity mContext, List<PhotoBean> list) {
        this.context = mContext;
        this.photoList = list;
        if (null != list) {
            int size = list.size() > MAX_NUMBER ? MAX_NUMBER : list.size();

            for (int i = 0; i < size; i++) {
                ImageView imageView = imageViewAry[i];
                PhotoBean entity = list.get(i);
                if (entity.isShowPhoto()) {
                    bgArr[i].setVisibility(View.VISIBLE);

                    if (entity.getPhotoType() == PhotoBean.DEFAULT_PHOTO) {
                        imageView.setBackgroundResource(R.mipmap.img_add_picture_bg);
                        addViewAry[i].setVisibility(View.VISIBLE);

                    } else if (entity.getPhotoType() == PhotoBean.URL_PHOTO) {
                        if (null != entity.getUri())
                            Glide.with(context).load(entity.getUri()).asBitmap().into(imageView);
                        else if (null != entity.getPath() && !TextUtils.isEmpty(entity.getPath())) {
                            File picture = new File(entity.getPath());
                            if (picture.exists()) {
                                //override(100, 100)
                                Glide.with(context).load(picture).asBitmap().into(imageView);
                                addViewAry[i].setVisibility(View.GONE);
                            }


                        }
                    }
                } else {
                    bgArr[i].setVisibility(View.INVISIBLE);
                }


            }
        } else {
            parentView.setVisibility(View.GONE);
        }
    }


    public List<PhotoBean> getData() {
        return photoList;
    }

    /**
     * 图片点击事件响应
     */
    private void addListener() {
        if (null != imageViewAry) {
            for (int i = 0; i < imageViewAry.length; i++) {
                ImageView imageView = imageViewAry[i];
                imageView.setTag(i);
                imageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (null != photoViewClickListener)
                            photoViewClickListener.onPhotoViewClick(view);
                    }
                });
            }
        }


    }

    public interface OnPhotoViewClickListener {
        public void onPhotoViewClick(View view);
    }


}
